package ac.neec.mio.ui.fragment;

import java.util.List;

import ac.neec.mio.R;
import ac.neec.mio.resting.RestingMeasurementManager;
import ac.neec.timewatch.MeasurementTimer;
import ac.neec.timewatch.TimeWatchInterface;
import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by G011C1093 on 14/01/22.
 */
public class RestingDialogFragment extends DialogFragment implements TimeWatchInterface {

	private TimeWatchInterface restingMeasurementInterface = null;
	private Button cancelButton;
	private Dialog dialog;
	private TextView dialogTime;
	private MeasurementTimer measurementTimer;
	private String measurementTime;
	private TimeWatchInterface fragment;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case RestingMeasurementManager.TIMER_RUNNING:
				dialogTime.setText(measurementTime);
				break;
			case RestingMeasurementManager.TIMER_STOP:
				dialog.dismiss();
			}
		}
	};

	public RestingDialogFragment(List<TimeWatchInterface> list) {
		measurementTimer = new MeasurementTimer(
				RestingMeasurementManager.RESTING_MEASUREMENT_TIME);
		setObsever(list);
	}

	private void setObsever(List<TimeWatchInterface> list) {
		measurementTimer.addObservers(this);
		for(TimeWatchInterface obj : list){
			measurementTimer.addObservers(obj);
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.dialog_measurement);
		setDialog();
		initFindViews();
		setLinsteners();
		start();
		return dialog;
	}

	private void setDialog() {
		setCancelable(false);
		dialog.setTitle(R.string.resting_measurement_dialog_title);

		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	private void start() {
		measurementTimer.startTimer();
	}

	private void initFindViews() {
		dialogTime = (TextView) dialog.findViewById(R.id.dialog_time);
		cancelButton = (Button) dialog.findViewById(R.id.btnCancel);
	}

	private void setLinsteners() {
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				restingMeasurementInterface.onCancel();
				measurementTimer.cancel();
			}
		});
	}

	public void setRetingDialogListener(TimeWatchInterface restingMeasurementInterface) {
		this.restingMeasurementInterface = restingMeasurementInterface;
	}

	public void removeRetingDialogListener() {
		this.restingMeasurementInterface = null;
	}

	private void setMeasurementTime(long time) {
		time /= 10;
		long sec = time / 100;
		long dec = time % 100;
		StringBuffer sb = new StringBuffer();

		sb.append(sec);
		sb.append(RestingMeasurementManager.DECIMAL_POINT);
		if (dec < 10)
			sb.append(0);
		sb.append(dec);
		measurementTime = String.valueOf(sb);
	}

	@Override
	public void notifyTimerUpdata(long time) {
		setMeasurementTime(time);
		handler.sendEmptyMessage(RestingMeasurementManager.TIMER_RUNNING);

	}

	@Override
	public void onCancel() {
		handler.sendEmptyMessage(RestingMeasurementManager.TIMER_STOP);
		Log.e("", "onCancel!!!");

	}

	@Override
	public void onAlarm() {
		handler.sendEmptyMessage(RestingMeasurementManager.TIMER_STOP);
		Log.e("", "onAlarm!!!");
	}
}
